<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############


$keyname = preg_replace('/[^a-zA-Z0-9]/', '', get('css'));


$THEME->addBreadcrumb($this->adminUrl.'/edit-stylesheet/?css='.$keyname, $this->lang('edit_stylesheet'));
$THEME->addBreadcrumb($this->adminUrl.'/edit-stylesheet/?css='.$keyname, $this->getContentLang('title', $keyname));

$mData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}jfxcss_stylesheets WHERE keyname = '{$keyname}'");

if($mData===false){
    JFX::addError($this->lang('invalid_keyname'));
}else{

    if(Antz_IntelliForm::submitted('edit-menu')){
        $error = false;

        $keyname = preg_replace('/[^a-zA-Z0-9]/', '', post('keyname'));
        $title = post('title');
        $css = $_POST['css'];
        $default = (int) post('default');
        $version = post('version');
        if(trim($keyname)==''){
            $error = true;
            JFX::addError($this->lang('invalid_keyname'));
        }

        if($version == 'v_all') $version = '';

        if($keyname != $mData['keyname'] && $DB->countRows($CONFIG->dbprefix.'jfxcss_stylesheets', "keyname = '{$keyname}'")>0){
            $error = true;
            JFX::addError($this->lang('keyname_already_registered'));
            JFX::addFormError('keyname');
        }

        if(!$error){
            $dbParams = array(
                'keyname' => $keyname,
                'is_default' => $default,
                'browser_version' => $version
            );
            $this->updateContentLang('title', $keyname, $title, '*', '*');
            $DB->update($CONFIG->dbprefix.'jfxcss_stylesheets', $dbParams, "keyname = '{$mData['keyname']}'");

            $fh = fopen($this->dirname.'/files/'.$mData['id'].'.css', 'w');
            fwrite($fh, $css);
            fclose($fh);

            if($keyname != $mData['keyname']){
                $DB->query("UPDATE {$CONFIG->dbprefix}jfxcss_pages_to_stylesheets SET css_key = '{$keyname}' WHERE css_key = '{$mData['keyname']}'");
            }
            JFX::addSuccess($this->lang('success'));
        }




    }

$mData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}jfxcss_stylesheets WHERE keyname = '{$keyname}'");


$versionOpts = array(''=>$this->lang('v_all'));
foreach($this->browserVersions as $k=>$v){
    $versionOpts[$v] = $this->lang('v_'.$v);
}


    $formFields = array(
        array(
            'type' => 'text',
            'name' => 'keyname',
            'value' => post('keyname', $mData['keyname']),
            'label' => $this->lang('keyname')
        ),
        array(
            'type' => 'text',
            'name' => 'title',
            'value' => post('title', $this->getContentLang('title', $mData['keyname'])),
            'label' => $this->lang('title')
        ),
        array(
            'type' => 'checkbox',
            'name' => 'default',
            'value' => '1',
            'checked' => (bool) post('default', $mData['is_default']),
            'label' => $this->lang('default_for_new_pages')
        ),
        array(
            'type' => 'select',
            'name' => 'version',
            'value' => post('version', $mData['browser_version']),
            'options' => $versionOpts,
            'label' => $this->lang('browser_version')
        ),
        array(
            'type' => 'code',
            'id' => 'jfxmenunew',
            'class' => 'css',
            'name' => 'css',
            'value' => post('css', file_get_contents($this->dirname.'/files/'.$mData['id'].'.css'), 500000),
            'label' => $this->lang('css')
        ),
        array(
            'type' => 'submit',
            'name' => 'submitBtn',
            'value' => $this->lang('submit'),
            'label' => '&nbsp;'
        )
    );






    echo JFX::makeRapidForm($formFields, $this->lang('edit_legend'), 'edit-menu');
}
